String.prototype.substrByCharLen = function(len) {
	var length = 0;
	for ( var i = 0; i < this.length; i++) {
		iCode = this.charCodeAt(i);
		if ((iCode >= 0 && iCode <= 255)
				|| (iCode >= 0xff61 && iCode <= 0xff9f)) {
			length += 1;
		} else {
			length += 2;
		}

		if (length >= len) {
			return this.substring(0, i) + '...';
		}
	}

	return this;
};

function substringByCharLen(str, len) {
	var length = 0;
	for ( var i = 0; i < str.length; i++) {
		iCode = str.charCodeAt(i);
		if ((iCode >= 0 && iCode <= 255)
				|| (iCode >= 0xff61 && iCode <= 0xff9f)) {
			length += 1;
		} else {
			length += 2;
		}

		if (length >= len) {
			return str.substring(0, i) + '...';
		}
	}

	return str;
}

function resizeImgX(img, iwidth, iheight) {
	var _img = new Image();
	_img.src = img.src;
	if (_img.width > _img.height) {
		img.width = (_img.width > iwidth) ? iwidth : _img.width;
		img.height = (_img.height / _img.width) * img.width;
	} else if (_img.width < _img.height) {
		img.height = (_img.height > iheight) ? iheight : _img.height;
		img.width = (_img.width / _img.height) * img.height;
	} else {
		img.height = (_img.height > iheight) ? iheight : _img.height;
		img.width = (_img.width > iwidth) ? iwidth : _img.width;
	}
}

function secondsToTime(seconds) {
	var hour = Math.floor(seconds / 3600);
	var left = seconds - hour * 3600;
	var minute = Math.floor(left / 60);
	var second = left - minute * 60;
	if (minute < 10) {
		minute = '0' + minute;
	}
	if (second < 10) {
		second = '0' + second;
	}

	return hour + ':' + minute + ':' + second;
}

function rmbFormat(fen) {
	var big = Math.floor(fen / 100);
	var small = fen % 100;
	if (small < 10) {
		small = '0' + small;
	}

	if (isNaN(big)) {
		big = '--';
	}
	if (isNaN(small)) {
		small = '--';
	}

	return big + '.' + small;
}

function setElementValueByID(id, value) {
	var element = document.getElementById(id);
	if (element != null) {
		element.value = value;
	}
}

function getElementValueByID(id) {
	var element = document.getElementById(id);
	if (element != null) {
		return element.value;
	}
	return '';
}

function setElementInnerHtmlByID(id, value) {
	var element = document.getElementById(id);
	if (element != null) {
		element.innerHTML = value;
	}
}

function loadPageSection(theURL, containerID, data) {
	$.ajax({
		type : 'post',
		url : theURL,
		data : data,
		dataType : 'html',
		success : function(data, state, xmlhttp) {
			var container = document.getElementById(containerID);
			if (container != null) {
				container.innerHTML = data;
				goScript(data);
			}
		},
		beforeSend: function(xhr) {
			openWaitingDialog('waitingDialog');
		},
		complete : function(xmlhttpRequest, textStatus) {
			closeWaitingDialog('waitingDialog');
			hideFocus('A');
		}
	});
}

function goScript(ajaxLoadedData) {
	var regDetectJs = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
	var jsContained = ajaxLoadedData.match(regDetectJs);

	if (jsContained) {
		var regGetJS = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;

		var jsNums = jsContained.length;
		for ( var i = 0; i < jsNums; i++) {
			var jsSection = jsContained[i].match(regGetJS);

			if (jsSection[2]) {
				if (window.execScript) {
					window.execScript(jsSection[2]);
				} else {
					window.eval(jsSection[2]);
				}
			}
		}
	}
}

function hideFocus(tName) {
	aTag = document.getElementsByTagName(tName);
	if (window.ActiveXObject) {
		for (i = 0; i < aTag.length; i++) {
			aTag[i].hideFocus = true;
		}
	} else {
		for (i = 0; i < aTag.length; i++) {
			aTag[i].onfocus = function() {
				this.blur();
			};
		}
	}
}

function openWaitingDialog(dialogID) {
	$
			.dialog({
				id : dialogID,
				content : '正在刷新，请稍候...<img src=\"/safe_manage/images/waiting.gif\" width="18" height="18" />',
				lock : false,
				esc : false,
				closeText : ''
			});
}

function closeWaitingDialog(dialogID) {
	try {
		$.dialog.get(dialogID).close();
	} catch (e) {
	}
}

function isDigit(s) {
	var r, re;
	re = /\d*/i;
	r = s.match(re);
	return r == s;
}
